package training_camp.day4.leetcode142;

public class Solution {
    public ListNode detectCycle(ListNode head) {
        /**
         *  fast = 2*slow
         *  fast = a+b+(b+c)*n;
         *  slow = a + b ;
         *  a+ b = b+c
         *  a = c
         */
        ListNode slow = head;
        ListNode fast = head;
        while (fast!=null&&fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow){
                ListNode index = head;
                while(fast!= index){
                    fast = fast.next;
                    index = index.next;
                }
                return fast;
            }
        }
        return null ;
    }
}